package com.woniu40.petai_user.web;

import com.github.pagehelper.PageInfo;
import com.woniu40.petai_user.entity.Article;
import com.woniu40.petai_user.entity.Collect;
import com.woniu40.petai_user.entity.User;
import com.woniu40.petai_user.result.ResponseResult;
import com.woniu40.petai_user.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("article")
public class ArticleController {

    @Autowired
    ArticleService articleService;

    //页面展示四篇文章
    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1") Integer currentPage,
                       @RequestParam(defaultValue = "4") Integer pageSize,
                       String articleContent,
                       @RequestParam(defaultValue = "0") Integer artTypeId,
                       @RequestParam(defaultValue = "0") Integer type,
                       Map map) {
        if ("null".equals(articleContent)) {
            articleContent = "";
        }
        if ("undefined".equals(articleContent)) {
            articleContent = "";
        }
        PageInfo<Article> pageInfo = articleService.findByInput(currentPage, pageSize, articleContent, artTypeId, type);
        //得到前4
        List<Article> ArtTopList = articleService.findArtTop();
        map.put("ArtTopList", ArtTopList);
        map.put("pageInfo", pageInfo);
        map.put("artTypeId", artTypeId);
        map.put("articleContent", articleContent);
        map.put("type", type);
        map.put("articleContent", articleContent);
        return "article";
    }

    @RequestMapping("/artcleDetails")
    public String artcleDetails(Integer articleId, Integer userId, Map map) {
        Article article = articleService.findArtById(articleId);
        Integer isCollect = 0;
        if (userId == null || userId == 0) {
            map.put("isCollect", isCollect);
        } else {
            Collect findcol = articleService.findcol(articleId, userId);
            if (findcol == null) {
                map.put("isCollect", isCollect);
            } else {
                isCollect = findcol.getCollectId();
                map.put("isCollect", isCollect);
            }
        }
        articleService.updateView(articleId);
        map.put("article", article);
        return "artcleDetails";
    }

    @RequestMapping("/addCol")
    @ResponseBody
    public ResponseResult<String> addcollect(HttpSession session,
                                             @RequestParam("articleId") Integer articleId) {

        User user = (User) session.getAttribute("user");
        if (user == null) {
            ResponseResult<String> result = new ResponseResult<>(101, "未登录，无法收藏", "未登录，无法收藏");
            return result;
        } else {
            Date date = new Date();
            Timestamp timestamp = new Timestamp(date.getTime());
            articleService.addNewCollectInfo(user.getUserId(), articleId, timestamp);
            ResponseResult<String> result = new ResponseResult<>(200, "添加收藏成功", "添加收藏成功");
            return result;
        }
    }

    @RequestMapping("/removeCol")
    @ResponseBody
    public ResponseResult<String> removeCol(@RequestParam("articleId") Integer articleId,
                                            @RequestParam("isCollect") Integer isCollect) {
        articleService.removeNewCollectInfo(isCollect, articleId);
        ResponseResult<String> result = new ResponseResult<>(200, "删除收藏成功", "删除收藏成功");
        return result;
    }

    @RequestMapping("/hot")
    @ResponseBody
    public ResponseResult<List<Article>> findHotArtical(){
        List<Article> artTop = articleService.findArtTop();
        ResponseResult<List<Article>> result = new ResponseResult<>(200,"最多收藏",artTop);
        return result;
    }

}
